import {Component, OnInit, ViewChild} from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap';
import {AddMail, AddDelivery, AddEMS, AddExtraMail, AddExtraDelivery, AddExtraEMS, EditExpress} from '../delivery';
import {ActivatedRoute} from '@angular/router';
import {AddressService} from '../../../../lib/service/address.service';
import {LocalStorageService} from '../../../../lib/service/local-storage.service';
import {DeliveryManageService} from '../delivery-manage.service';

@Component({
  selector: 'app-delivery-edit',
  templateUrl: 'delivery-edit.component.html',
  styleUrls: ['delivery-edit.component.scss'],
  providers: [DeliveryManageService]
})
export class DeliveryEditComponent implements OnInit {

  applet_uuid: number;  // 关联小程序uuid
  type: string;
  message: string;
  isShow = false;

  provinceList = [];
  cityList = [];
  regionList = [];
  provinceCode = 0;  // 选中省code
  cityCode = 0;
  regionCode = 0;
  delivery_name: string;  // 模板名称
  type_mail: boolean;
  type_delivery: boolean;
  type_EMS: boolean;
  mailList: any;
  deliveryList: any;
  EMSList: any;
  addMail = new AddMail;
  addDelivery = new AddDelivery;
  addEMS = new AddEMS;
  addExtraMail = new AddExtraMail;
  addExtraDelivery = new AddExtraDelivery;
  addExtraEMS = new AddExtraEMS;
  editExpress = new EditExpress;
  extraMailRange = [];
  extraDeliveryRange = [];
  extraEMSRange = [];
  editExpressRange = [];
  addProvinceList = [];
  checkedProvince: string; // 当前选中的省名
  selfAdd = [];  // 自定义添加地区
  isEdit: boolean;  // 是否在编辑中添加地区
  operateId: number;  // 操作的模板ID
  operateDefault: boolean;  // 操作的模板是否属于默认模板
  operateType: number;  // 操作的模板类型 0:平邮 1:快递 2:EMS
  loadingShow = true;

  express_template_id: number;  // 运费模板ID

  @ViewChild('cityModal') public cityModal: ModalDirective;
  @ViewChild('deleteModal') public deleteModal: ModalDirective;
  @ViewChild('editModal') public editModal: ModalDirective;

  constructor(private route: ActivatedRoute,
              private addressService: AddressService,
              private localStorageService: LocalStorageService,
              private deliveryManageService: DeliveryManageService) {
  }

  ngOnInit() {
    this.getExpress();
  }

  getAddressDetail(code) {
    this.addressService.getAddressDetail(code).subscribe(
      res => {
        if (res.data) {
          if (res.data.province) {
            this.provinceCode = res.data.province.code;
            this.getCityList(res.data.province.code);
          }
          if (res.data.city) {
            this.cityCode = res.data.city.code;
            this.getRegionList(res.data.city.code);
          }
          if (res.data.region) {
            this.regionCode = res.data.region.code;
          }
        }
        this.getProvinceList();
      }
    );
  }

  getProvinceList() {
    this.addressService.getProvinceList().subscribe(
      res => {
        if (res.code === 2000) {
          this.provinceList = res.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  getCityList(provinceCode) {
    this.addressService.getCityList(provinceCode).subscribe(
      res => {
        if (res.code === 2000) {
          this.cityList = res.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  getRegionList(cityCode) {
    this.addressService.getRegionList(cityCode).subscribe(
      res => {
        if (res.code === 2000) {
          this.regionList = res.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 选择省之后
  changeProvince() {
    this.cityCode = 0;
    this.regionCode = 0;
    this.regionList = [];
    this.addressService.getCityList(this.provinceCode).subscribe(
      res => {
        if (res.code === 2000) {
          this.cityList = res.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 选择市之后
  changeCity() {
    this.regionCode = 0;
    this.addressService.getRegionList(this.cityCode).subscribe(
      res => {
        if (res.code === 2000) {
          this.regionList = res.data;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 获取运费模板
  getExpress() {
    this.loadingShow = true;
    this.express_template_id = this.route.snapshot.params['id'];
    this.applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    this.deliveryManageService.getExpressTempDetail(this.express_template_id).subscribe(
      res => {
        setTimeout(
          () => {
            this.loadingShow = false;
          }
          ,500);
        this.delivery_name = res.data.name;
        this.getAddressDetail(res.data.where_goods_at);
        this.getExpressTempList('平邮', 'mailList', 'type_mail');
        this.getExpressTempList('快递', 'deliveryList', 'type_delivery');
        this.getExpressTempList('EMS', 'EMSList', 'type_EMS');
      }
    );
  }

  // 获取运费模板内容
  getExpressTempList(typeStr: string, container: string, type: string) {
    this.deliveryManageService.getExpressTempValList(this.express_template_id, typeStr).subscribe(
      res => {
        this[container] = res.data.data;
        if (!!res.data.data.length) {
          this[type] = true;
          this[container].every((val, idx, array) => {
            val.starting_weight_price = val.starting_weight_price/1000;
            val.exceed_weight_price = val.exceed_weight_price/1000;
            return true;
          })
        }
      }
    );
  }

  tempValueSubmit() {
    // 提交模板name
    const data = {
      id: this.express_template_id,
      name: this.delivery_name,
      applet_uuid: this.applet_uuid,
      where_goods_at: this.regionCode
    };
    this.deliveryManageService.editExpressTemp(data).subscribe(
      res => {
        if (res.code === 2000) {
          // 提交模板内容
          this.type = 'success';
          this.message = '模板名称修改成功';
          this.isShow = true;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 提交平邮模板内容
  tempMailSubmit(T: boolean) {
    if (!T) {
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addMail.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addMail).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.getExpressTempList('平邮', 'mailList', 'type_mail');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 提交快递模板内容
  tempDeliverySubmit(T: boolean) {
    if (!T) {
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addDelivery.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addDelivery).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.getExpressTempList('快递', 'deliveryList', 'type_delivery');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 提交EMS模板内容
  tempEMSSubmit(T: boolean) {
    if (!T) {
      this.type = 'info';
      this.message = '请将信息填写完整';
      this.isShow = true;
      return;
    }
    this.addEMS.express_template_id = this.express_template_id;
    this.deliveryManageService.addExpressTempVal(this.addEMS).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.getExpressTempList('EMS', 'EMSList', 'type_EMS');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 提交指定地区的模板内容
  tempExtraSubmit(type: number) {
    let data;
    if (type === 0) {
      this.addExtraMail.express_template_id = this.express_template_id;
      if (!this.extraMailRange.length) {
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraMail.range = JSON.stringify(this.extraMailRange);
      data = this.addExtraMail;
    }
    if (type === 1) {
      this.addExtraDelivery.express_template_id = this.express_template_id;
      if (!this.extraDeliveryRange.length) {
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraDelivery.range = JSON.stringify(this.extraDeliveryRange);
      data = this.addExtraDelivery;
    }
    if (type === 2) {
      this.addExtraEMS.express_template_id = this.express_template_id;
      if (!this.extraEMSRange.length) {
        this.type = 'info';
        this.message = '请选择配送区域';
        this.isShow = true;
        return;
      }
      this.addExtraEMS.range = JSON.stringify(this.extraEMSRange);
      data = this.addExtraEMS;
    }
    this.deliveryManageService.addExpressTempVal(data).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '添加成功';
          this.isShow = true;
          this.getExpressTempList('平邮', 'mailList', 'type_mail');
          this.getExpressTempList('快递', 'deliveryList', 'type_delivery');
          this.getExpressTempList('EMS', 'EMSList', 'type_EMS');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  addCityDialog(type: number, isEdit?: boolean) {
    this.operateType = type;
    this.addressService.getProvinceList().subscribe(
      res => {
        if (res.code === 2000) {
          if (this.operateType === 0) {
            this.selfAdd = this.extraMailRange;
          }
          if (this.operateType === 1) {
            this.selfAdd = this.extraDeliveryRange;
          }
          if (this.operateType === 2) {
            this.selfAdd = this.extraEMSRange;
          }
          if (isEdit) {
            this.isEdit = true;
            this.selfAdd = this.editExpressRange;
          } else {
            this.isEdit = false;
          }
          this.addProvinceList = res.data;
          this.cityModal.show();
        }
      }
    );
  }

  // 添加
  addSelf() {
    for (let i = 0; i < this.selfAdd.length; i++) {
      if (this.checkedProvince === this.selfAdd[i]) {
        return;
      }
    }
    this.selfAdd.push(this.checkedProvince);
  }

  // 删除模板内容
  delValDialog(id: number) {
    this.operateId = id;
    this.deleteModal.show();
  }

  delSubmit() {
    this.deliveryManageService.delExpressTempVal(this.operateId).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '删除成功';
          this.isShow = true;
          this.deleteModal.hide();
          this.getExpressTempList('平邮', 'mailList', 'type_mail');
          this.getExpressTempList('快递', 'deliveryList', 'type_delivery');
          this.getExpressTempList('EMS', 'EMSList', 'type_EMS');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }

  // 编辑弹窗
  editDialog(id: number, isDefault: number) {
    this.operateId = id;
    this.operateDefault = !isDefault;
    this.deliveryManageService.getExpressTempValDetail(id).subscribe(
      res => {
        if (res.code === 2000) {
          this.editExpress = res.data;
          this.editExpress.starting_weight_price = this.editExpress.starting_weight_price/1000;
          this.editExpress.exceed_weight_price = this.editExpress.exceed_weight_price/1000;
          this.editExpressRange = JSON.parse(res.data.range);
          this.editModal.show();
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }

      }
    );
  }

  // 提交编辑表单
  editSubmit() {
    this.editExpress.range = JSON.stringify(this.editExpressRange);
    this.deliveryManageService.editExpressTempVal(this.editExpress).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '修改成功';
          this.isShow = true;
          this.editModal.hide();
          this.getExpressTempList('平邮', 'mailList', 'type_mail');
          this.getExpressTempList('快递', 'deliveryList', 'type_delivery');
          this.getExpressTempList('EMS', 'EMSList', 'type_EMS');
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }
}
